package com.qianniu.napi.admin.web;

import akka.actor.ActorRef;
import akka.actor.Props;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.*;
import com.qianniu.napi.admin.mapper.PintuanOpenMapper;
import com.qianniu.napi.admin.service.*;
import com.qianniu.napi.admin.service.akka.AkkaServer;
import com.qianniu.napi.admin.service.akka.ResourceActor;
import com.qianniu.napi.common.config.Constants;
import com.qianniu.napi.common.util.ObjectUtil;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.*;

/**
 * <p>
 * 分类导航
 * </p>
 *
 * @author chenxuebin
 * @since 2018-10-17
 */
@RestController
public class NavController {

    private static final Logger logger = LoggerFactory.getLogger(NavController.class);

    @Autowired
    private INavService navService;
    @Autowired
    private IBannerService bannerService;
    @Autowired
    private INoticeService noticeService;
    @Autowired
    private IGoodsCategoryService gcService;
    @Autowired
    private IMerchantConfigService mcService;
    @Autowired
    private ICouponTplService couponTplService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private ICutpriceService cutpriceService;
    @Autowired
    private IPintuanService pintuanService;
    @Autowired
    private PintuanOpenMapper pintuanOpenMapper;
    @Autowired
    private IGoodsCategoryService goodscategoryService;
    @Autowired
    private RedissonClient redis;
    @Autowired
    private IRedisService redisService;
    @Autowired
    private IStoreService storeService;
    @Autowired
    private IConfigService configService;
    @Autowired
    private IMconfigExtService mconfigExtService;
    @Autowired
    private IDisplayFenleiService displayFenleiService;

    @RequestMapping("/api/nav/list")
    @ResponseBody
    public Jinfo list(@RequestParam(value = "status", required = false) String status,
                      @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                      @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        Jinfo info = new Jinfo();
        if (pageSize == null) pageSize = 10;
        Page page = new Page(pageIndex, pageSize);
        page = navService.selectPage(page, new EntityWrapper<Nav>()
                .eq("mid", ObjectUtil.getMid())
                .eq("state", 1)
                .orderBy("sort", true));
        info.setInfo(page.getRecords());
        return info;
    }

    @RequestMapping("/api/app/init")
    @ResponseBody
    public Jinfo appInit(@RequestParam(value = "v") Long version,
                         @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                         @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        logger.debug("=================api/app/init");

        Jinfo info = new Jinfo();
        MerchantConfig mc = redisService.getMconfig();
//        Map map = new HashMap<String, Object>();
        IndexInfo map = new IndexInfo();
        Long mid = ObjectUtil.getMid();
        Long mpid = ObjectUtil.getMpid();

        String rkey = Constants.REDIS_APP_INIT + mpid;
        RBucket<IndexInfo> bucket = redis.getBucket(rkey);
        logger.debug("========rkey=" + rkey);
        if (bucket.isExists() && bucket.get() != null) {
            logger.debug("========rkey value=" + bucket.get());
            info.setInfo(bucket.get());
            return info;
        }

        // 首页配置
        map.setMset(mc);

        // banners
        if (mc.getIsBanner() == 1) {
            List banners = bannerService.selectList(new EntityWrapper<Banner>()
                    .eq("mid", ObjectUtil.getMid())
                    .eq("mpid", mpid).isNull("hyid")
                    .eq("state", 1).orderBy("sort", true));
            if (banners != null) map.setBanners(banners);
        }

        // 首页分类导航
        if (mc.getIsNav() == 1) {
            Page page = new Page(1, 30);
            Page<Nav> list = navService.selectPage(page, new EntityWrapper<Nav>()
                    .eq("mid", ObjectUtil.getMid())
                    .eq("mpid", mpid)
                    .eq("state", 1)
                    .orderBy("sort", true));
            logger.info(">>>>>navlist.size=" + list.getRecords().size());
//            map.put("navs", list.getRecords());
            map.setNavs(list.getRecords());
        }

        // 首页券默认显示
        List<CouponTpl> qlist = couponTplService.getHomeCoupons(mpid, ObjectUtil.getUid());
        if (qlist != null && qlist.size() > 0) {
            map.setQuans(qlist);
        }

        // 商家介绍
        if (mc.getIsMinfo() == 1) {
            Store store = storeService.selectOne(new EntityWrapper<Store>().eq("mid", mid)
                    .eq("mpid", mpid));
            if (store != null) {
                Store minfo = new Store();
                minfo.setAddress(store.getAddress());
                minfo.setPhone(store.getPhone());
                minfo.setLinkman(store.getLinkman());
                minfo.setName(store.getName());
                minfo.setInfo(store.getInfo());

                minfo.setProvince(store.getProvince());
                minfo.setCity(store.getCity());
                minfo.setDistrict(store.getDistrict());
                minfo.setIsParking(store.getIsParking());
                minfo.setIsWifi(store.getIsWifi());
                minfo.setIsWxpay(store.getIsWxpay());
                minfo.setIsZfbpay(store.getIsZfbpay());

                minfo.setLatitude(store.getLatitude());
                minfo.setLongitude(store.getLongitude());

//                map.put("minfo", minfo);
                map.setMinfo(minfo);
            }
        }

        // 首页显示通知
        if (mc.getIsNotice() == 1) {
            Wrapper<Notice> entity = new EntityWrapper<Notice>();
            entity.eq("mid", ObjectUtil.getMid());
            entity.eq("mpid", mpid);
            entity.eq("state", 1);
            entity.orderBy("id", false);
            List<Notice> list = noticeService.selectList(entity);
            if (list != null && list.size() > 0) {
//                map.put("notices", list);
                map.setNotices(list);
            }
        }

        // 商家分类
        if (mc.getIsHomeCategory() == 1) {
            Wrapper<GoodsCategory> wrapper = new EntityWrapper<GoodsCategory>();
            wrapper.eq("mid", mid);
            wrapper.eq("mpid", mpid);
            wrapper.eq("state", 1);
            wrapper.eq("level", 1);
            wrapper.orderBy("paixu", true);

            List<GoodsCategory> categories = goodscategoryService.selectList(wrapper);
//            if (categories != null) map.put("categories", categories);
            if (categories != null) map.setCategories(categories);
        }

        // 商品
        Page page = new Page(1, 100);

        Wrapper<Goods> wrapper = new EntityWrapper<Goods>();
        wrapper.eq("mpid", mpid)
                .eq("state", 1).orderBy("id", false);
        logger.info("=======add state=1 filter");
        Page<Goods> list = goodsService.selectPage(page, wrapper);
        List<Goods> glist = list.getRecords();

        List<Goods> baokuan = new ArrayList<Goods>();
        List<Goods> tuijian = new ArrayList<Goods>();
        List<Goods> goods = new ArrayList<Goods>();
        List<Goods> pintuan = new ArrayList<Goods>();
        List<Goods> kanjia = new ArrayList<Goods>();
        List<Goods> choujiang = new ArrayList<Goods>();

        int bkcout = 0;
        int tjcout = 0;
        int gcout = 0;
        int ptcout = 0;
        int kjcout = 0;
        int cjcout = 0;
        pageSize = pageSize == null ? 10 : pageSize;
        for (Goods item : glist) {
            if (item.getType() == 1) {
                ptcout++;
                if (ptcout <= pageSize) pintuan.add(item);
            } else if (item.getType() == 2) {
                kjcout++;
                if (kjcout <= pageSize) kanjia.add(item);
            } else if (item.getType() == 3) {
                cjcout++;
                if (cjcout <= pageSize) choujiang.add(item);
            } else if (item.getIsHot() == 1) {
                bkcout++;
                if (bkcout <= pageSize) baokuan.add(item);
            } else if (item.getIsRecommend() == 1) {
                tjcout++;
                if (tjcout <= pageSize) tuijian.add(item);
            } else {
                gcout++;
                if (gcout <= pageSize) goods.add(item);
            }
        }

        if (bkcout > 0) map.setBaokuan(baokuan);
        if (tjcout > 0) map.setTuijian(tuijian);
        if (gcout > 0) map.setGoods(goods);

        if (ptcout > 0) map.setPintuan(pintuan);
        if (kjcout > 0) map.setKanjia(kanjia);
        if (cjcout > 0) map.setChoujiang(choujiang);

//        List<Cutprice> cuts = cutpriceService.selectList(new EntityWrapper<Cutprice>().eq("mid", mid).eq("state", 1).eq("status", 2).orderBy("id", false));
//        if (cuts != null && cuts.size() > 0) {
////            map.put("cuts", cuts);
//            map.setCuts(cuts);
//        }

        Map<String, Object> qmap = new HashMap<String, Object>();
//        qmap.put("mid", mid);
        qmap.put("status", 2);
        qmap.put("size", 10);
        qmap.put("skip", 0);

        if (mc.isCached()) bucket.set(map);
        info.setInfo(map);
        return info;
    }

    @RequestMapping("/api/mapp/init")
    @ResponseBody
    public Jinfo mappInit(@RequestParam(value = "v") Long version,
                          @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                          @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        logger.debug("=======111=======api/app/init");

        Jinfo info = new Jinfo();
        MerchantConfig mc = redisService.getMconfig();
//        Map map = new HashMap<String, Object>();
        IndexInfo map = new IndexInfo();
        Map<String, List<Goods>> fenleiMap = new HashMap<String, List<Goods>>();
        Map<String, String> showMap = new HashMap<String, String>();
        Map<String, String> showHnameMap = new HashMap<String, String>();
        Map<String, Integer> showListMap = new HashMap<String, Integer>();
        Map<String, Integer> showHeadMap = new HashMap<String, Integer>();

        Long mid = ObjectUtil.getMid();
        Long mpid = ObjectUtil.getMpid();

        String rkey = Constants.REDIS_APP_INIT + mpid;
        RBucket<IndexInfo> bucket = redis.getBucket(rkey);
        logger.debug("========rkey=" + rkey);
        if (bucket.isExists() && bucket.get() != null) {
            logger.debug("========rkey value=" + bucket.get());
            info.setInfo(bucket.get());
            return info;
        }

        // 首页配置
        map.setMset(mc);

        // banners
        if (mc.getIsBanner() == 1) {
            List banners = bannerService.selectList(new EntityWrapper<Banner>().eq("mid", ObjectUtil.getMid())
                    .eq("mpid", mpid).isNull("hyid")
                    .eq("state", 1).orderBy("sort", true));
            if (banners != null) map.setBanners(banners);
        }

        // 首页分类导航
        if (mc.getIsNav() == 1) {
            Page page = new Page(1, 30);
            Page<Nav> list = navService.selectPage(page, new EntityWrapper<Nav>()
                    .eq("mid", ObjectUtil.getMid())
                    .eq("mpid", mpid)
                    .eq("state", 1)
                    .orderBy("sort", true));
            logger.info(">>>>>navlist.size=" + list.getRecords().size());
//            map.put("navs", list.getRecords());
            map.setNavs(list.getRecords());
        }

        // 首页券默认显示
        List<CouponTpl> qlist = couponTplService.getHomeCoupons(mpid, ObjectUtil.getUid());
        if (qlist != null && qlist.size() > 0) {
            map.setQuans(qlist);
        }

        // 商家介绍
        if (mc.getIsMinfo() == 1) {
            Store store = storeService.selectOne(new EntityWrapper<Store>().eq("mid", mid)
                    .eq("mpid", mpid));
            if (store != null) {
                Store minfo = new Store();
                minfo.setAddress(store.getAddress());
                minfo.setPhone(store.getPhone());
                minfo.setLinkman(store.getLinkman());
                minfo.setName(store.getName());
                minfo.setInfo(store.getInfo());

                minfo.setProvince(store.getProvince());
                minfo.setCity(store.getCity());
                minfo.setDistrict(store.getDistrict());
                minfo.setIsParking(store.getIsParking());
                minfo.setIsWifi(store.getIsWifi());
                minfo.setIsWxpay(store.getIsWxpay());
                minfo.setIsZfbpay(store.getIsZfbpay());

                minfo.setLatitude(store.getLatitude());
                minfo.setLongitude(store.getLongitude());

//                map.put("minfo", minfo);
                map.setMinfo(minfo);
            }
        }

        // 首页显示通知
        if (mc.getIsNotice() == 1) {
            Wrapper<Notice> entity = new EntityWrapper<Notice>();
            entity.eq("mid", ObjectUtil.getMid());
            entity.eq("mpid", mpid);
            entity.eq("state", 1);
            entity.orderBy("id", false);
            List<Notice> list = noticeService.selectList(entity);
            if (list != null && list.size() > 0) {
//                map.put("notices", list);
                map.setNotices(list);
            }
        }

        // 商家分类
        if (mc.getIsHomeCategory() == 1) {
            Wrapper<GoodsCategory> wrapper = new EntityWrapper<GoodsCategory>();
            wrapper.eq("mid", mid);
            wrapper.eq("mpid", mpid);
            wrapper.eq("state", 1);
            wrapper.eq("level", 1);
            wrapper.orderBy("paixu", true);

            List<GoodsCategory> categories = goodscategoryService.selectList(wrapper);
//            if (categories != null) map.put("categories", categories);
            if (categories != null) map.setCategories(categories);
        }

        // 商品
        Page page = new Page(1, 100);

        Wrapper<Goods> wrapper = new EntityWrapper<Goods>();
        wrapper.eq("mpid", mpid)
                .eq("state", 1).orderBy("id", false);
        logger.info("=======add state=1 filter");


        List<Goods> baokuan = new ArrayList<Goods>();
        List<Goods> tuijian = new ArrayList<Goods>();
        List<Goods> goods = new ArrayList<Goods>();
        List<Goods> pintuan = new ArrayList<Goods>();
        List<Goods> kanjia = new ArrayList<Goods>();
        List<Goods> choujiang = new ArrayList<Goods>();

        int bkcout = 0;
        int tjcout = 0;
        int gcout = 0;
        int ptcout = 0;
        int kjcout = 0;
        int cjcout = 0;
        pageSize = pageSize == null ? 10 : pageSize;

        List<DisplayFenlei> fenleis = displayFenleiService.selectList(
                new EntityWrapper<DisplayFenlei>().eq("mpid", mpid)
                        .eq("state", 1)
                        .orderBy("sort", true));

        if (fenleis != null && fenleis.size() > 0) {
            page = new Page(1, 10);
//                设置分类，按照分类显示
            int m=0;
            for (int n = 0; n < fenleis.size(); n++) {
                List<Goods> fllist = goodsService.selectPage(page, new EntityWrapper<Goods>()
                        .eq("mpid", mpid)
                        .eq("state", 1)
                        .eq("gdcid", fenleis.get(n).getId())
                        .orderBy("sort")
                        .orderBy("id", false)
                ).getRecords();

                if (fllist != null && fllist.size() > 0) {
                    fenleiMap.put("fenlei" + m, fllist);
                    showListMap.put("fenlei" + m , fenleis.get(n).getListtype());
                    showHeadMap.put("fenlei" + m , fenleis.get(n).getHeadType());
                    showMap.put("fenlei" + m , fenleis.get(n).getShowname());
                    showHnameMap.put("fenlei" + m , fenleis.get(n).getPic());
                    m++;
                }
            }
            map.setDisplayFenlei(fenleiMap);
            map.setShowNameMap(showMap);
            map.setShowHnameMap(showHnameMap);
            map.setShowHeadMap(showHeadMap);
            map.setShowListMap(showListMap);
        } else {
            page = new Page(1, 100);
            Page<Goods> list = goodsService.selectPage(page, wrapper);
            List<Goods> glist = list.getRecords();

            for (Goods item : glist) {
                if (item.getType() == 1) {
                    ptcout++;
                    if (ptcout <= pageSize) pintuan.add(item);
                } else if (item.getType() == 2) {
                    kjcout++;
                    if (kjcout <= pageSize) kanjia.add(item);
                } else if (item.getType() == 3) {
                    cjcout++;
                    if (cjcout <= pageSize) choujiang.add(item);
                } else if (item.getIsHot() == 1) {
                    bkcout++;
                    if (bkcout <= pageSize) baokuan.add(item);
                } else if (item.getIsRecommend() == 1) {
                    tjcout++;
                    if (tjcout <= pageSize) tuijian.add(item);
                } else {
                    gcout++;
                    if (gcout <= pageSize) goods.add(item);
                }
            }
        }

        if (bkcout > 0) map.setBaokuan(baokuan);
        if (tjcout > 0) map.setTuijian(tuijian);
        if (gcout > 0) map.setGoods(goods);

        if (ptcout > 0) map.setPintuan(pintuan);
        if (kjcout > 0) map.setKanjia(kanjia);
        if (cjcout > 0) map.setChoujiang(choujiang);

//        List<Cutprice> cuts = cutpriceService.selectList(new EntityWrapper<Cutprice>().eq("mid", mid).eq("state", 1).eq("status", 2).orderBy("id", false));
//        if (cuts != null && cuts.size() > 0) {
////            map.put("cuts", cuts);
//            map.setCuts(cuts);
//        }

        if (mc.isCached()) bucket.set(map);

        // 读取OEM信息，判断自己当前商户的贴牌商，其次读取OEM库
//        info.setOEM(redisService.getOem(ObjectUtil.getGuid()));
        info.setInfo(map);
        return info;
    }

    @RequestMapping("/api/app/minit")
    @ResponseBody
    public Jinfo appMinit(@RequestParam(value = "v") Long version,
                          @RequestParam(value = "uv",required = false) Long uversion) {
        logger.debug("================================api/app/minit");
        Jinfo info = new Jinfo();
        MerchantConfig mc = redisService.getMconfig();
        IndexInfo map = new IndexInfo();
        Long mid = ObjectUtil.getMid();
        Long mpid = ObjectUtil.getMpid();

        String rkey = Constants.REDIS_APP_INIT + mpid;
        RBucket<IndexInfo> bucket = redis.getBucket(rkey);
        logger.debug("========rkey=" + rkey);
        MconfigExt mconfigExt = mconfigExtService.selectById(mpid);
        logger.debug("========================mconfigExt=" + JSON.toJSONString(mconfigExt));

        if (bucket.isExists() && bucket.get() != null) {
            logger.debug("========rkey value=" + bucket.get());
            map = bucket.get();
            mc = map.getMset();
            if(mconfigExt!=null&&mconfigExt.getRuzhu()!=null&&uversion!=null&&uversion.longValue()<=mconfigExt.getVersion().longValue()){
                mc.setRuzhu(mconfigExt.getRuzhu());
            }else{
                mc.setRuzhu(-1);
            }
            map.setMset(mc);
            info.setInfo(map);
            return info;
        }

        if(mconfigExt!=null&&mconfigExt.getRuzhu()!=null&&uversion!=null&&uversion.longValue()<=mconfigExt.getVersion().longValue()){
            mc.setRuzhu(mconfigExt.getRuzhu());
        }

        // 首页配置
        map.setMset(mc);
        // banners
        if (mc.getIsBanner() != null && mc.getIsBanner() == 1) {
            List banners = bannerService.selectList(new EntityWrapper<Banner>().eq("mid", ObjectUtil.getMid()).eq("mpid", mpid)
                    .eq("state", 1).isNull("hyid").orderBy("sort", true));
            if (banners != null) map.setBanners(banners);
        }

        // 首页分类导航
        if (mc.getIsNav() != null && mc.getIsNav() == 1) {
            Page page = new Page(1, 60);
            Page<Nav> list = navService.selectPage(page, new EntityWrapper<Nav>()
                    .setSqlSelect("jianchen,name,pic,ntype,nvalue")
                    .eq("mpid", mpid)
                    .eq("state", 1)
                    .orderBy("sort", true));
            logger.info(">>>>>navlist.size=" + list.getRecords().size());
//            map.put("navs", list.getRecords());
            map.setNavs(list.getRecords());
        }

        // 首页券默认显示
        if (mc.getIsHomeQuan() != null && mc.getIsHomeQuan() == 1) {
            List<CouponTpl> qlist = couponTplService.getHomeCoupons(mpid, ObjectUtil.getUid());
            if (qlist != null && qlist.size() > 0) {
                map.setQuans(qlist);
            }
        }

        // 首页显示通知
        if (mc.getIsNotice() != null && mc.getIsNotice() == 1) {
            Wrapper<Notice> entity = new EntityWrapper<Notice>();
            entity.eq("mpid", ObjectUtil.getMpid());
            entity.eq("state", 1);
            entity.orderBy("id", false);
            List<Notice> list = noticeService.selectList(entity);
            if (list != null && list.size() > 0) {
//                map.put("notices", list);
                map.setNotices(list);
            }
        }

        // 首页附近商家
        if (mc.isCached()) bucket.set(map);
        info.setInfo(map);
        return info;
    }

    @RequestMapping("/api/app/pinit")
    @ResponseBody
    public Jinfo appPinit(@RequestParam(value = "v") Long version,
                          @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                          @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        logger.debug("========api/app/pinit");

        Jinfo info = new Jinfo();
        MerchantConfig mc = redisService.getMconfig();
        IndexInfo map = new IndexInfo();
        Long mpid = ObjectUtil.getMpid();

        String rkey = Constants.REDIS_APP_INIT + mpid;
        RBucket<IndexInfo> bucket = redis.getBucket(rkey);
        logger.debug("========rkey=" + rkey);
        if (bucket.isExists() && bucket.get() != null) {
            logger.debug("========rkey value=" + bucket.get());
            info.setInfo(bucket.get());
            return info;
        }

        // 首页配置
        map.setMset(mc);

        // banners
        if (mc.getIsBanner() == 1) {
            List banners = bannerService.selectList(new EntityWrapper<Banner>().eq("mid", ObjectUtil.getMid()).eq("state", 1)
                    .isNull("hyid").orderBy("sort", true));
            if (banners != null) map.setBanners(banners);
        }

        // 首页分类导航
        if (mc.getIsNav() == 1) {
            Page page = new Page(1, 30);
            Page<Nav> list = navService.selectPage(page, new EntityWrapper<Nav>()
                    .eq("mid", ObjectUtil.getMid())
                    .eq("state", 1)
                    .orderBy("sort", true));
            logger.info(">>>>>navlist.size=" + list.getRecords().size());
//            map.put("navs", list.getRecords());
            map.setNavs(list.getRecords());
        }

        // 首页券默认显示
//        List<CouponTpl> qlist = couponTplService.getHomeCoupons(mid, ObjectUtil.getUid());
//        if (qlist != null && qlist.size() > 0) {
////            map.put("quans", qlist);
//            map.setQuans(qlist);
//        }

        // 首页显示通知
        if (mc.getIsNotice() == 1) {
            Wrapper<Notice> entity = new EntityWrapper<Notice>();
            entity.eq("mid", ObjectUtil.getMid());
            entity.eq("state", 1);
            entity.orderBy("id", false);
            List<Notice> list = noticeService.selectList(entity);
            if (list != null && list.size() > 0) {
//                map.put("notices", list);
                map.setNotices(list);
            }
        }


        if (mc.isCached()) bucket.set(map);
        info.setInfo(map);
        return info;
    }

    @RequestMapping("/api/app/index")
    @ResponseBody
    public String appIndex(@RequestParam(value = "v") Long version,
                           @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                           @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        Jinfo info = new Jinfo();
        MerchantConfig mc = redisService.getMconfig();
        Map map = new HashMap<String, Object>();
        Long mid = ObjectUtil.getMid();
        Long mpid = ObjectUtil.getMpid();
        String rkey = Constants.REDIS_APP_INIT + mpid;
        RBucket<String> bucket = redis.getBucket(rkey);
        logger.debug("========rkey=" + rkey);
        if (bucket.isExists() && !Strings.isNullOrEmpty(bucket.get())) {

            logger.debug("========rkey value=" + bucket.get());
            return bucket.get();
        }

        // 首页配置
        map.put("mset", mc);

        // banners
        if (mc.getIsBanner() == 1) {
            List banners = bannerService.selectList(new EntityWrapper<Banner>().eq("mid", ObjectUtil.getMid())
                    .isNull("hyid").eq("state", 1).orderBy("sort", true));
            if (banners != null) map.put("banners", banners);
        }

        // 首页分类导航
        if (mc.getIsNav() == 1) {
            Page page = new Page(1, 30);
            Page<Nav> list = navService.selectPage(page, new EntityWrapper<Nav>()
                    .eq("mid", ObjectUtil.getMid())
                    .eq("state", 1)
                    .orderBy("sort", true));
            logger.info(">>>>>navlist.size=" + list.getRecords().size());
            map.put("navs", list.getRecords());
        }

        // 首页券默认显示
        List<CouponTpl> qlist = couponTplService.getHomeCoupons(mid, ObjectUtil.getUid());
        if (qlist != null && qlist.size() > 0) {
            map.put("quans", qlist);
        }

        // 商家介绍
        if (mc.getIsMinfo() == 1) {
            Store store = storeService.selectOne(new EntityWrapper<Store>().eq("mid", mid).eq("mpid", mpid));
            if (store != null) {
                Store minfo = new Store();
                minfo.setAddress(store.getAddress());
                minfo.setPhone(store.getPhone());
                minfo.setLinkman(store.getLinkman());
                minfo.setName(store.getName());
                minfo.setInfo(store.getInfo());

                minfo.setProvince(store.getProvince());
                minfo.setCity(store.getCity());
                minfo.setDistrict(store.getDistrict());
                minfo.setIsParking(store.getIsParking());
                minfo.setIsWifi(store.getIsWifi());
                minfo.setIsWxpay(store.getIsWxpay());
                minfo.setIsZfbpay(store.getIsZfbpay());

                minfo.setLatitude(store.getLatitude());
                minfo.setLongitude(store.getLongitude());

                map.put("minfo", minfo);
            }
        }

        // 首页显示通知
        if (mc.getIsNotice() == 1) {
            Wrapper<Notice> entity = new EntityWrapper<Notice>();
            entity.eq("mid", ObjectUtil.getMid());
            entity.eq("state", 1);
            entity.orderBy("id", false);
            List<Notice> list = noticeService.selectList(entity);
            if (list != null && list.size() > 0) {
                map.put("notices", list);
            }
        }

        // 商家分类
        if (mc.getIsHomeCategory() == 1) {
            Wrapper<GoodsCategory> wrapper = new EntityWrapper<GoodsCategory>();
            wrapper.eq("mid", mid);
            wrapper.eq("state", 1);
            wrapper.eq("level", 1);
            wrapper.orderBy("paixu", true);

            List<GoodsCategory> categories = goodscategoryService.selectList(wrapper);
            if (categories != null) map.put("categories", categories);
        }

        // 商品
        Page page = new Page(1, 50);

        Wrapper<Goods> wrapper = new EntityWrapper<Goods>();
        wrapper.eq("mid", mid).orderBy("id", false);
        Page<Goods> list = goodsService.selectPage(page, wrapper);
        List<Goods> glist = list.getRecords();

        List<Goods> baokuan = new ArrayList<Goods>();
        List<Goods> tuijian = new ArrayList<Goods>();
        List<Goods> goods = new ArrayList<Goods>();
        int bkcout = 0;
        int tjcout = 0;
        int gcout = 0;
        pageSize = pageSize == null ? 10 : pageSize;
        for (Goods item : glist) {
            if (item.getIsHot() == 1) {
                bkcout++;
                if (bkcout <= pageSize) baokuan.add(item);
            } else if (item.getIsRecommend() == 1) {
                tjcout++;
                if (tjcout <= pageSize) tuijian.add(item);
            } else {
                gcout++;
                if (gcout <= pageSize) goods.add(item);
            }
        }

        if (bkcout > 0) map.put("baokuan", baokuan);
        if (tjcout > 0) map.put("tuijian", tuijian);
        if (gcout > 0) map.put("goods", goods);

        List<Cutprice> cuts = cutpriceService.selectList(new EntityWrapper<Cutprice>().eq("mid", mid).eq("state", 1).eq("status", 2).orderBy("id", false));
        if (cuts != null && cuts.size() > 0) {
            map.put("cuts", cuts);
        }

        Map<String, Object> qmap = new HashMap<String, Object>();
        qmap.put("mid", mid);
        qmap.put("status", 2);
        qmap.put("size", 10);
        qmap.put("skip", 0);

        List<PintuanOpen> pintuan = pintuanOpenMapper.selectPTOpenPage(qmap);
//        Date now = new Date();
//        for(PintuanOpen item: list){
//            if(status==0){
//                item.setShowCountdownMilliseconds(item.getStarttime().getTime()-now.getTime());
//            }else if(status==1){
//                item.setShowCountdownMilliseconds(item.getEndtime().getTime()-now.getTime());
//            }
//        }
        if (pintuan != null && pintuan.size() > 0) {
            map.put("pintuan", pintuan);
        }

        info.setInfo(map);
        String result = JSON.toJSONString(info);
        if (mc.isCached()) bucket.set(result);
        return result;
    }

    @RequestMapping("/api/app/getoem")
    @ResponseBody
    public Jinfo getoem(@Valid Nav nav,
                        BindingResult bindingResult, HttpServletRequest req) {

        Jinfo info = new Jinfo();
        info.setInfo(redisService.getOem(ObjectUtil.getMpid(), ObjectUtil.getGuid()));
        return info;
    }

    @RequestMapping("/api/app/getoemphone")
    @ResponseBody
    public Jinfo getoemphone() {

        Jinfo info = new Jinfo();
        info.setInfo(redisService.getOemphone(ObjectUtil.getMpid(), ObjectUtil.getGuid()));
        return info;
    }

    @RequestMapping("/api/nav/add")
    @ResponseBody
    public Jinfo navAdd(@Valid Nav nav,
                        BindingResult bindingResult) {
        Jinfo info = new Jinfo();
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            info.setMsg("There are errors! " + bindingResult);
            info.setCode(400);
            return info;
        }
        nav.setCreatetime(new Date());
        nav.setMid(ObjectUtil.getMid());
        nav.setMpid(ObjectUtil.getMpid());
        nav.setUid(ObjectUtil.getUid());
        nav.insert();
        //更新资源文件
        AkkaServer.system.actorOf(Props.create(ResourceActor.class))
                .tell("Nav@" + nav.getId() + "@insert", ActorRef.noSender());
        return info;
    }

    @RequestMapping("/api/nav/modify")
    @ResponseBody
    public Jinfo navUpdate(HttpServletRequest req,
                           Nav nav) {
        Jinfo info = new Jinfo();
        nav.setModifytime(new Date());
        nav.updateById();
        //更新资源文件
        AkkaServer.system.actorOf(Props.create(ResourceActor.class))
                .tell("Nav@" + nav.getId() + "@update", ActorRef.noSender());
        return info;
    }

    @RequestMapping("/api/nav/detail")
    @ResponseBody
    public Jinfo getNavDetailById(@RequestParam("token") String token,
                                  @RequestParam("id") long id, HttpServletRequest req
    ) {
        Jinfo info = new Jinfo();
        Nav nav = navService.selectById(id);
        info.setInfo(nav);
        return info;
    }

    @RequestMapping("/api/app/navs")
    @ResponseBody
    public Jinfo initnavs() {
        logger.debug("================================api/app/minit");
        Jinfo info = new Jinfo();

        Page page = new Page(1,50);
        Page<Nav> list = navService.selectPage(page, new EntityWrapper<Nav>()
                .setSqlSelect("jianchen,name,pic,ntype,nvalue")
                .eq("mpid", ObjectUtil.getMpid())
                .eq("state", 1)
                .orderBy("sort", true));
        logger.info(">>>>>navlist.size=" + list.getRecords().size());

        info.setInfo(list.getRecords());
        return info;
    }

    //平台入驻行业选择接口
    //适用于商户助手端
    @RequestMapping("/api/app/hangye")
    @ResponseBody
    public Jinfo inithangye(@RequestParam("asmpid") Long mpid) {
        logger.debug("================================api/app/minit");
        Jinfo info = new Jinfo();

        Page page = new Page(1,50);
        Page<Config> list = configService.selectPage(page, new EntityWrapper<Config>()
                .setSqlSelect("id,code,name")
                .eq("mpid", mpid)
                .eq("type", "hangyefenlei")
                .orderBy("sort", true));
        logger.info(">>>>>navlist.size=" + list.getRecords().size());

        List<Config> configs = new ArrayList<Config>();
        Config conf = new Config();
        conf.setName("请选择行业");
        conf.setId(0l);
        conf.setCode("");
        configs.add(conf);
        configs.addAll(list.getRecords());
        info.setInfo(configs);

        return info;
    }

}
